package cn.lx.test;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.URI;

/**
 * Created by Administrator on 2017/3/17.
 */
public class RestTest1 {
    public static final String BASE_URI = "http://localhost:8080/";
    private WebTarget webTarget;
    private HttpServer  server;
    @Before
    public void init() throws IOException {
        ResourceConfig resourceConfig = new ResourceConfig().packages("cn.lx");
        URI uri = URI.create(BASE_URI);
        server =GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig);
        server.start();
        Client client = ClientBuilder.newClient(new ClientConfig());
        webTarget = client.target(BASE_URI).path("test");

    }


    @After
    public void shutDown(){
        if(server != null ){
            server.shutdown();
        }
    }

    @Test
    public void test1(){
        WebTarget path = webTarget.path("/test1");
        Invocation.Builder request = path.request();
        Response response = request.get();
        System.out.println(response.readEntity(String.class));
    }

    @Test
    public void test2(){
        WebTarget path = webTarget.path("/test2");
        Invocation.Builder request = path.request();
        Response response = request.get();
        System.out.println(response.readEntity(String.class)+">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }
}

